﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.IO;
using WSServer.Interfaces;
using BartonExtension;
using Cache;
namespace WSServer
{
   
    public class WSStorage : IWSStorage
    {
        private string diskPathID { get; set; }

       
        public WSStorage(string localDiskPathID)
        {
            this.diskPathID = localDiskPathID;
        }
        public override bool store(List<WSResponse> obj,string storeId)
        {
           /* var socket_cacheStoragePath = HttpRuntime.AppDomainAppPath + "{0}\\".FormatWith(this.diskPathID);
            if(!Directory.Exists(socket_cacheStoragePath))
            {
                Directory.CreateDirectory(socket_cacheStoragePath);    
            }
            try
            {
                SerializeManager.SerializeToFile(obj, socket_cacheStoragePath + storeId + ".socketcache");
                return true;
            }
            catch(Exception e)
            {
                return false;
            }*/

            int size = Cache.CacheManager.instance.doCacheObject(obj, storeId).retryCount;
            if(size!=0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override List<WSResponse> loadFromStorage(string storeId)
        {
           // var socket_cacheStoragePath = HttpRuntime.AppDomainAppPath + "{0}\\".FormatWith(this.diskPathID);
        //    return //SerializeManager.DeserializeFromFile<List<WSResponse>>(socket_cacheStoragePath + storeId + ".socketcache");
            return Cache.CacheManager.instance.objectFromCache<List<WSResponse>>(storeId);

        }
        public override bool cleanup(string storeId)
        {
          /*  var socket_cacheStoragePath = HttpRuntime.AppDomainAppPath + "{0}\\".FormatWith(this.diskPathID) + storeId + ".socketcache";
            try
            {
                File.Delete(socket_cacheStoragePath);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }*/
            return Cache.CacheManager.instance.RemoveCache(storeId);
        }
    }
}
